package com.alks.function.service.impl.stockdept.pcfactorystockquery;

import com.alks.common.data.dto.SysUserTokenDTO;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.entity.data.entity.PcStitchCollectDeptDetail;
import com.alks.entity.data.entity.PcStitchCollectDeptHead;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.request.stockdept.pcfactorystockwork.StcColDetRequest;
import com.alks.function.data.request.stockdept.pcfactorystockwork.StcColHeadRequest;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcStitchCollectDeptDetailMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcStitchCollectDeptHeadMapper;
import com.alks.function.service.stockdept.pcfactorystockquery.IStcColDefService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.alks.entity.data.enums.ResultCodeEnum.INPUT_ERROR;

@Service
@Slf4j
@RequiredArgsConstructor
public class StcColDefServiceImpl extends ServiceImpl<SD_SQ_PcStitchCollectDeptHeadMapper, PcStitchCollectDeptHead> implements IStcColDefService {
    private final SD_SQ_PcStitchCollectDeptHeadMapper headMapper;
    private final SD_SQ_PcStitchCollectDeptDetailMapper detailMapper;

    @Override
    public ResponseInfo getStcColHeadQuery() {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取数据*/
        List<PcStitchCollectDeptHead> dtos = lambdaQuery()
                .eq(PcStitchCollectDeptHead::getCompanyId,companyId).list();
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo stcColHeadAdd(StcColHeadRequest request) {
        SysUserTokenDTO sysUserTokenDTO = UserIdThread.get();
        /*判断组别ID是否重复*/
        Integer count = lambdaQuery()
                .eq(PcStitchCollectDeptHead::getCollectDept, request.getCollectDeptNew()).count();
        if (count > 0) {
            return ResponseInfo.error("组别代码重复", INPUT_ERROR.getCode());
        }
        /*获取写入数据*/
        PcStitchCollectDeptHead head = BeanUtil.copyBean(request, PcStitchCollectDeptHead.class);
        head.setCollectDept(request.getCollectDeptNew());
        head.setCompanyId(sysUserTokenDTO.getCompanyId());
        /*新增*/
        headMapper.insert(head);
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo stcColHeadUpdate(StcColHeadRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        /*判断入参合法性*/
        if (request.getFactoryDept() == null) {
            return ResponseInfo.error("缴库部门不能为空");
        }
        if (request.getCollectDept() == null) {
            return ResponseInfo.error("组别代码不能为空");
        }
        if (request.getGroupDept() == null) {
            return ResponseInfo.error("部门分类不能为空");
        }
        if (request.getPersons() == null) {
            return ResponseInfo.error("人数不能为空");
        }
        /*判断修改后的部门ID是否重复*/
        Integer count = lambdaQuery()
                .eq(PcStitchCollectDeptHead::getCollectDept, request.getCollectDeptNew()).count();
        if (count > 0) {
            return ResponseInfo.error("组别代码重复", INPUT_ERROR.getCode());
        }
        /*更新数据*/
        lambdaUpdate()
                .set(PcStitchCollectDeptHead::getPersons, request.getPersons())
                .set(PcStitchCollectDeptHead::getGroupDept, request.getGroupDept())
                .set(PcStitchCollectDeptHead::getVendorFlag, request.getVendorFlag())
                .set(PcStitchCollectDeptHead::getFactoryDept, request.getFactoryDept())
                .set(PcStitchCollectDeptHead::getCollectName, request.getCollectName())
                .set(PcStitchCollectDeptHead::getCollectDept, request.getCollectDeptNew())
                .eq(PcStitchCollectDeptHead::getCompanyId,companyId)
                .eq(PcStitchCollectDeptHead::getCollectDept, request.getCollectDept()).update();
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo stcColHeadDelete(String collectDept) {
        String companyId = UserIdThread.get().getCompanyId();
        /*删除对应数据*/
        LambdaQueryWrapper<PcStitchCollectDeptHead> wrapper = new LambdaQueryWrapper<PcStitchCollectDeptHead>()
                .eq(PcStitchCollectDeptHead::getCompanyId,companyId)
                .eq(PcStitchCollectDeptHead::getCollectDept, collectDept);
        headMapper.delete(wrapper);
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo getStcColDetailQuery(String collectDept) {
        String companyId = UserIdThread.get().getCompanyId();
        /*查询数据*/
        LambdaQueryWrapper<PcStitchCollectDeptDetail> wrapper = new LambdaQueryWrapper<PcStitchCollectDeptDetail>()
                .eq(PcStitchCollectDeptDetail::getCollectDept, collectDept)
                .eq(PcStitchCollectDeptDetail::getCompanyId,companyId);
        List<PcStitchCollectDeptDetail> dtos = detailMapper.selectList(wrapper);
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo stcColDetailAdd(StcColDetRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        /*判断新组名是否重复*/
        LambdaQueryWrapper<PcStitchCollectDeptDetail> wrapperQ=new LambdaQueryWrapper<PcStitchCollectDeptDetail>()
                .eq(PcStitchCollectDeptDetail::getCollectDept,request.getCollectDept())
                .eq(PcStitchCollectDeptDetail::getCompanyId,companyId)
                .eq(PcStitchCollectDeptDetail::getDeptNo,request.getDeptNoNew());
        if (detailMapper.selectCount(wrapperQ)>0){
            return ResponseInfo.error("组别代码重复", INPUT_ERROR.getCode());
        }
        /*获取待写入数据*/
        PcStitchCollectDeptDetail detail = BeanUtil.copyBean(request, PcStitchCollectDeptDetail.class);
        detail.setDeptNo(request.getDeptNoNew());
        detail.setDeptName(request.getDeptName());
        detail.setCompanyId("BF");
        /*写入数据*/
        detailMapper.insert(detail);
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo stcColDetailUpdate(StcColDetRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        /*判断新组名是否重复*/
        LambdaQueryWrapper<PcStitchCollectDeptDetail> wrapperQ=new LambdaQueryWrapper<PcStitchCollectDeptDetail>()
                .eq(PcStitchCollectDeptDetail::getCollectDept,request.getCollectDept())
                .eq(PcStitchCollectDeptDetail::getDeptNo,request.getDeptNoNew())
                .eq(PcStitchCollectDeptDetail::getCompanyId,companyId);
        if (detailMapper.selectCount(wrapperQ)>0){
            return ResponseInfo.error("组别代码重复", INPUT_ERROR.getCode());
        }
        /*更新数据*/
        LambdaUpdateWrapper<PcStitchCollectDeptDetail> wrapperU = new LambdaUpdateWrapper<PcStitchCollectDeptDetail>()
                .eq(PcStitchCollectDeptDetail::getCollectDept, request.getCollectDept())
                .eq(PcStitchCollectDeptDetail::getDeptNo, request.getDeptNo())
                .eq(PcStitchCollectDeptDetail::getCompanyId,companyId)
                .set(PcStitchCollectDeptDetail::getDeptName, request.getDeptName())
                .set(PcStitchCollectDeptDetail::getDeptNo, request.getDeptNoNew());
        detailMapper.update(null,wrapperU);
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo stcColDetailDelete(String collectDept, String deptNo) {
        String companyId = UserIdThread.get().getCompanyId();
        /*删除指定数据*/
        LambdaQueryWrapper<PcStitchCollectDeptDetail> wrapperQ=new LambdaQueryWrapper<PcStitchCollectDeptDetail>()
                .eq(PcStitchCollectDeptDetail::getCollectDept,collectDept)
                .eq(PcStitchCollectDeptDetail::getCompanyId,companyId)
                .eq(PcStitchCollectDeptDetail::getDeptNo,deptNo);
        detailMapper.delete(wrapperQ);
        return ResponseInfo.ok();
    }
}
